from django.contrib import admin
from models import *
from django.contrib import databrowse


#class StatusProductAdmin(admin.ModelAdmin):
#    pass

class TemplateComponentAdmin(admin.ModelAdmin):
    model = TemplateComponent
    #list_filter = ('supplier',)    
    search_fields = ['name',  'description', 'supplier__name', 'supplier__description']
    list_display = ('name', 'description','supplier')
    
class ActionProductInline(admin.TabularInline): # StackedInline
    model = ActionProduct


class ComponentInline(admin.TabularInline):
    model = Component
    fieldsets = (
        (None, {
            'fields': ('serialnumber',  'name', 'mac_address')
        }),
    ) 


class ProductAdmin(admin.ModelAdmin):
    #save_as = True
    save_on_top = True
    inlines = [ActionProductInline, ComponentInline]
    model = Product
    date_hierarchy = 'purchase_date'
    #raw_id_fields = ('name',)
    """fieldsets = (
        (None, {
            'fields': ('serialnumber', 'name',  'status', 'sale_price', 'delivery_note' )
        }),
        ('Advanced options', {
            'classes': ('collapse',),
            'fields': ('description', 'version', 'installdate',
                       'purchase_date', 'purchase_order', 'future_location')
        }),
    )"""
    #list_display_links = ('name', 'cid', 'status')
    #filter_vertical = ('purchase_date')
    search_fields = ['serialnumber', 'name__name', 'description']
    list_filter = ('version', 'installdate',   'status', 'name', 'lot', 'owner', 'location_at_owner')
    list_display = ('serialnumber', 'name',  'status','owner', 'location_at_owner' , 'version')
    
    class Media:
                js = ('/static/js/enableDisableInputsButton.js',
				'/static/js/jquery.selectboxes.js',
				'/static/js/selectProductOwner.js', )
    
    

class CompanyAdmin(admin.ModelAdmin):    
    model = Company
    fieldsets = (
        (None, {
            'fields': ('name', 'address', 'zip', 'town',
                       'language', 'country')
        }),
        ('Advanced options', {
            'classes': ('collapse',),
            'fields': ('distance', 'site', 'campus')
        }),
    )
    list_filter = ('language', 'country')
    search_fields = ['name', 'address', 'zip', 'town']
    #list_select_related = ('language', 'country')
    list_display = ('name', 'address', 'zip', 'town', 'country')
    
class ActionInline(admin.TabularInline): # StackedInline
    model = Action 
    #raw_id_fields = ('component',)
    
    
class ComponentAdmin(admin.ModelAdmin):
    save_as = True
    save_on_top = True
    raw_id_fields = ('psn',)  
    inlines = [ActionInline] 
    """fieldsets = (
        (None, {
            'fields': ('serialnumber', 'psn', 'name', 'status', 'date_of_purchase', 'purchase_price' , 'delivery_note', 'mac_address')
        }),
        ('Advanced options', {
            'classes': ('collapse',),
            'fields': ('description', 'software_version',
                       'software_upgrade',  'mac_address2',
                       'date_status_change')
        }),
    ) """
    
    list_filter = ('date_of_purchase', 'status')  
    search_fields = ['serialnumber', 'psn__serialnumber', 'psn__name__name', 'name__name', 'description'] # hier is join
    list_display = ('serialnumber', 'name', 'description',  'psn',  'status', 'date_of_purchase')
    
    class Media:
                js = ('/static/js/checkMacAddress.js', 
		'/static/js/enableDisableInputsButton.js')

class ActionStockComponentInline(admin.TabularInline): # StackedInline
    model = ActionStockComponent 


class StockComponentAdmin(admin.ModelAdmin):
    save_on_top = True
    inlines = [ActionStockComponentInline] 
    fieldsets = (
        (None, {
            'fields': ('referention',  'name', 'lot', 'date_of_purchase')
        }),
        ('Advanced options', {
            'classes': ('collapse',),
            'fields': ('description', 'stock_location'  )
        }),
    ) 
    
    list_filter = ('date_of_purchase',)  
    search_fields = ['referention', 'name__name', 'description'] # hier is join
    list_display = ('referention',  'name')
    
    class Media:        
        js = ('/static/js/totalStockComponents.js',)   
    
class TaskAdmin(admin.ModelAdmin):
    list_display = ('name', 'status', 'priority', 'scheduled_for' ,'note')
    list_filter = ('status', 'category',  'assigned_to', 'priority', 'created', 'completed', 'scheduled_for')
    search_fields = ['category__name', 'name', 'note'] 
    
class TaskCategoryAdmin(admin.ModelAdmin):
    list_display = ('name',  'description')
    list_filter = ('name',  'description')
    search_fields = ['name',  'description'] 
    
admin.site.register(TaskCategory, TaskCategoryAdmin)
admin.site.register(Task, TaskAdmin)
admin.site.register(StatusProduct)
admin.site.register(Supplier)
admin.site.register(TemplateComponent, TemplateComponentAdmin)
admin.site.register(Category)
admin.site.register(TemplateProduct)
admin.site.register(LocationAtOwner)
admin.site.register(Language)
admin.site.register(Country)
admin.site.register(Company, CompanyAdmin)
admin.site.register(Lot)
admin.site.register(Product, ProductAdmin)
admin.site.register(StatusComponent)
admin.site.register(TemplateAction)
admin.site.register(Component, ComponentAdmin)
admin.site.register(StockComponent, StockComponentAdmin)
#admin.site.register(Action, ActionAdmin)


databrowse.site.register(Product)
databrowse.site.register(StockComponent)
databrowse.site.register(Component)
